//
//  CookBookViewController.m
//  Taste
//
//  Created by 徐昊 on 16/9/3.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "CookBookViewController.h"
#import "HXMacro.h"
#import "CookBookCollectionViewCell.h"
#import "CookBookModel.h"
#import "ZWYNetTool.h"

#define kCookBookCollectionViewCellName @"CookBookCollectionViewCellNameReuse"
#define kViewWidth bounds.size.width
@interface CookBookViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (weak, nonatomic) IBOutlet UIView *redLine;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leading;

@property (nonatomic, strong) NSMutableArray *arrayOfButton;
@property (weak, nonatomic) IBOutlet UIButton *all;
@property (weak, nonatomic) IBOutlet UIButton *mainDish;
@property (weak, nonatomic) IBOutlet UIButton *headDish;
@property (weak, nonatomic) IBOutlet UIButton *soap;
@property (weak, nonatomic) IBOutlet UIButton *rice;
@property (weak, nonatomic) IBOutlet UIButton *dessert;
@property (weak, nonatomic) IBOutlet UIButton *juice;



@end

@implementation CookBookViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"大师菜谱";
    UIBarButtonItem *list = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"LZUniversal_BarButtonItem_Sort"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"LZUniversal_BarButtonItem_Search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    self.navigationItem.leftBarButtonItem = list;
    self.navigationItem.rightBarButtonItem = search;
    
    _layout.itemSize = CGSizeMake(SCREEN_WIDTH, _collectionView.bounds.size.height);
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing = 0;
    _collectionView.scrollEnabled = NO;
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.bounces = NO;
    _collectionView.pagingEnabled = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"CookBookCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kCookBookCollectionViewCellName];
    _arrayOfButton = [NSMutableArray array];
    _arrayOfButton = @[_all, _mainDish, _headDish, _soap, _rice, _dessert, _juice].mutableCopy;
    _all.selected = YES;
    for (int i = 0; i < _arrayOfButton.count; i++) {
        UIButton *btn = _arrayOfButton[i];
        btn.tag = i;
        [btn addTarget:self action:@selector(myClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}
- (void)myClick:(UIButton *)sender {
    for (UIButton *btn in _arrayOfButton) {
        btn.selected = NO;
    }
    sender.selected = YES;
    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH * sender.tag, 0);
    _leading.constant = _redLine.bounds.size.width * sender.tag;
    [UIView animateWithDuration:0.2 animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (void)click:(UIBarButtonItem *)button {
    
}
//- (IBAction)all:(UIButton *)sender {
//    for (UIButton *btn in _arrayOfButton) {
//        btn.selected = NO;
//    }
//    sender.selected = YES;
//    _collectionView.contentOffset = CGPointMake(0, 0);
//    _leading.constant = 0;
//    [UIView animateWithDuration:0.2 animations:^{
//        
//        [self.view layoutIfNeeded];
//    }];
//}
//- (IBAction)mainDish:(UIButton *)sender {
//    for (UIButton *btn in _arrayOfButton) {
//        btn.selected = NO;
//    }
//    sender.selected = YES;
//    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
//    _leading.constant = _redLine.bounds.size.width;
//    [UIView animateWithDuration:0.2 animations:^{
//        
//        [self.view layoutIfNeeded];
//    }];
//}
//- (IBAction)headDish:(UIButton *)sender {
//    for (UIButton *btn in _arrayOfButton) {
//        btn.selected = NO;
//    }
//    sender.selected = YES;
//    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH * 2, 0);
//    _leading.constant = _redLine.bounds.size.width * 2;
//    [UIView animateWithDuration:0.2 animations:^{
//        
//        [self.view layoutIfNeeded];
//    }];
//}
//- (IBAction)soap:(UIButton *)sender {
//    for (UIButton *btn in _arrayOfButton) {
//        btn.selected = NO;
//    }
//    sender.selected = YES;
//    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH * 3, 0);
//    _leading.constant = _redLine.bounds.size.width * 3;
//    [UIView animateWithDuration:0.2 animations:^{
//        
//        [self.view layoutIfNeeded];
//    }];
//}
//- (IBAction)rice:(UIButton *)sender {
//    for (UIButton *btn in _arrayOfButton) {
//        btn.selected = NO;
//    }
//    sender.selected = YES;
//    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH * 4, 0);
//    _leading.constant = _redLine.bounds.size.width * 4;
//    [UIView animateWithDuration:0.2 animations:^{
//        
//        [self.view layoutIfNeeded];
//    }];
//}
//- (IBAction)desset:(UIButton *)sender {
//    for (UIButton *btn in _arrayOfButton) {
//        btn.selected = NO;
//    }
//    sender.selected = YES;
//    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH * 5, 0);
//    _leading.constant = _redLine.bounds.size.width * 5;
//    [UIView animateWithDuration:0.2 animations:^{
//        
//        [self.view layoutIfNeeded];
//    }];
//}
//- (IBAction)juice:(UIButton *)sender {
//    for (UIButton *btn in _arrayOfButton) {
//        btn.selected = NO;
//    }
//    sender.selected = YES;
//    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH * 6, 0);
//    _leading.constant = _redLine.bounds.size.width * 6;
//    [UIView animateWithDuration:0.2 animations:^{
//        
//        [self.view layoutIfNeeded];
//    }];
//}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 7;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CookBookCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCookBookCollectionViewCellName forIndexPath:indexPath];
    cell.num = indexPath.row;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
